<?php

/**
 * This is the model class for table "zy_login_list".
 *
 * The followings are the available columns in table 'zy_login_list':
 * @property integer $login_list_id
 * @property integer $user_service_id
 * @property string $field1
 * @property string $field2
 * @property string $field3
 * @property string $field4
 * @property string $field5
 * @property string $field6
 * @property string $field7
 * @property string $field8
 */
class LoginList extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return LoginList the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'zy_login_list';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('user_service_id', 'required'),
			array('user_service_id', 'numerical', 'integerOnly'=>true),
			array('field1, field2, field3, field4, field5, field6, field7, field8', 'length', 'max'=>255),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('login_list_id, user_service_id, field1, field2, field3, field4, field5, field6, field7, field8', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		
		return array(
			'login_list_id' => Yii::t('base','User Name'),
			'user_service_id' => Yii::t('base','Service Name'),
			'field1' => Yii::t('base','Field1'),
			'field2' => Yii::t('base','Field2'),
			'field3' => Yii::t('base','Field3'),
			'field4' => Yii::t('base','Field4'),
			'field5' => Yii::t('base','Field5'),
			'field6' => Yii::t('base','Field6'),
			'field7' => Yii::t('base','Field7'),
			'field8' => Yii::t('base','Field8'),
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search($results)
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('login_list_id',LoginList::model()->getLoginlistId($this->login_list_id));
		$criteria->compare('user_service_id',LoginList::model()->getUserServiceId($this->user_service_id));
		$criteria->addInCondition('user_service_id',$results);
		$criteria->compare('field1',$this->field1,true);
		$criteria->compare('field2',$this->field2,true);
		$criteria->compare('field3',$this->field3,true);
		$criteria->compare('field4',$this->field4,true);
		$criteria->compare('field5',$this->field5,true);
		$criteria->compare('field6',$this->field6,true);
		$criteria->compare('field7',$this->field7,true);
		$criteria->compare('field8',$this->field8,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
				'sort'=>array(
						'defaultOrder'=>'login_list_id DESC',
				),
		));
	}
	public function search2()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.
	
		$criteria=new CDbCriteria;
		$user_service_id=yii::app()->request->getParam("user_service_id");
		$sql="select user_service_id from zy_user_service where user_id IN (select user_id from zy_user_service where user_service_id='".$user_service_id."')";
		$connection = Yii::app()->db;
		$command = $connection->createCommand($sql);
		$results = $command->queryAll();
		for($i=0;$i<count($results);$i++){
			$result[$i]=$results[$i]['user_service_id'];
		}
		$criteria->compare('login_list_id',LoginList::model()->getLoginlistId($this->login_list_id));
		$criteria->compare('user_service_id',LoginList::model()->getUserServiceId($this->user_service_id));
		$criteria->addInCondition('user_service_id',$result);
		$criteria->compare('field1',$this->field1,true);
		$criteria->compare('field2',$this->field2,true);
		$criteria->compare('field3',$this->field3,true);
		$criteria->compare('field4',$this->field4,true);
		$criteria->compare('field5',$this->field5,true);
		$criteria->compare('field6',$this->field6,true);
		$criteria->compare('field7',$this->field7,true);
		$criteria->compare('field8',$this->field8,true);
	
		return new CActiveDataProvider($this, array(
				'criteria'=>$criteria,
				'sort'=>array(
						'defaultOrder'=>'login_list_id DESC',
				),
		));
	}
	public function getLoginlistId($username)
	{
		if($username!="")
		{
			$sql = "SELECT login_list_id FROM zy_login_list WHERE user_service_id IN(SELECT user_service_id FROM zy_user_service WHERE user_id IN (SELECT user_id FROM zy_user WHERE user_name = '".$username."'))";
			$connection = Yii::app()->db;
	
			$command = $connection->createCommand($sql);
	
			$results = $command->queryAll();
	
			for($i=0;$i<count($results);$i++){
	
				$result[$i]=$results[$i]['login_list_id'];
	
			}
	
			return $result;
		}
	}
	public function getUserServiceId($servicename)
	
	{
	
		if($servicename!="")
	
		{
	
			$sql = "SELECT user_service_id FROM zy_login_list WHERE user_service_id IN(SELECT user_service_id FROM zy_user_service WHERE service_id IN (SELECT service_id FROM zy_service WHERE service_name = '".$servicename."'))";
	
			$connection = Yii::app()->db;
	
			$command = $connection->createCommand($sql);
	
			$results = $command->queryAll();
	
			for($i=0;$i<count($results);$i++){
	
				$result[$i]=$results[$i]['user_service_id'];
	
			}
	
			return $result;
	
		}
	
	}
	public function getUserName($id)
	{
		$rs = LoginList::model()->findBypk($id);
		$rs2 = UserService::model()->find('user_service_id='.$rs->user_service_id);
		if($rs2->user_service_id!=""){
			$rs3=User::model()->find('user_id='.$rs2->user_id);
			return $rs3->user_name;
		}
		
	}
	public function getVpnServerName($id,$field1)
	{
		$rs = LoginList::model()->findBypk($id);
		$rs2 = UserService::model()->find('user_service_id='.$rs->user_service_id);
		if($rs2->service_id==8){
			return Vpnlist::model()->findBypk($field1)->comment;
		}else{
			return $field1;
		}
		
	}
}